/***************************************************************
 * Name:      FileTransToolsMain.cpp
 * Purpose:   Code for Application Frame
 * Author:    xzhlksh (xzhlksh@gmail.com)
 * Created:   2021-08-08
 * Copyright: xzhlksh ()
 * License:
 **************************************************************/

#ifdef WX_PRECOMP
#include "wx_pch.h"
#endif

#ifdef __BORLANDC__
#pragma hdrstop
#endif //__BORLANDC__

#include "FileTransToolsMain.h"

//helper functions
enum wxbuildinfoformat {
    short_f, long_f };

wxString wxbuildinfo(wxbuildinfoformat format)
{
    wxString wxbuild(wxVERSION_STRING);

    if (format == long_f )
    {
#if defined(__WXMSW__)
        wxbuild << _T("-Windows");
#elif defined(__WXMAC__)
        wxbuild << _T("-Mac");
#elif defined(__UNIX__)
        wxbuild << _T("-Linux");
#endif

#if wxUSE_UNICODE
        wxbuild << _T("-Unicode build");
#else
        wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
    }

    return wxbuild;
}


FileTransToolsFrame::FileTransToolsFrame(wxFrame *frame)
    : GUIFrame(frame)
{
#if wxUSE_STATUSBAR
    m_statusBar->SetStatusText(_("Hello World!"), 0);
    m_statusBar->SetStatusText(_("Current Version : 1.2.0"), 1);
#endif
}

FileTransToolsFrame::~FileTransToolsFrame()
{
}

void FileTransToolsFrame::On_AppBtn_Clicked( wxCommandEvent& event )
{
      wxFileDialog * openFileDialog = new wxFileDialog(this, _("Open App file"), "", "",
                       "Hex files (*.hex)|*.hex", wxFD_OPEN|wxFD_FILE_MUST_EXIST);

  if (openFileDialog->ShowModal() == wxID_OK){
      wxString fileName = openFileDialog->GetPath();
      m_HexFileTrans.SetAppPath(fileName.ToStdWstring());
      m_AppText->SetValue(wxString(m_HexFileTrans.GetAppPath()));
  }

}

/** @brief (one liner)
  *
  * (documentation goes here)
  */
void FileTransToolsFrame::On_GenSW_Clicked(wxCommandEvent& event)
{
    bool retVal = m_HexFileTrans.GenSWFile();
    if(retVal == false)
    {
        wxMessageDialog * dial = new wxMessageDialog(NULL, m_HexFileTrans.GetLES(), _T("Error"), wxOK | wxICON_ERROR);
        dial->ShowModal();
    }
}

/** @brief (one liner)
  *
  * (documentation goes here)
  */
void FileTransToolsFrame::On_GenCal_Clicked(wxCommandEvent& event)
{
    bool retVal = m_HexFileTrans.GenCalFile();
    if(retVal == false)
    {
        wxMessageDialog * dial = new wxMessageDialog(NULL, m_HexFileTrans.GetLES(), _T("Error"), wxOK | wxICON_ERROR);
        dial->ShowModal();
    }
    else
    {
        m_CalText->SetValue(m_HexFileTrans.GetCalPath());
    }
}

/** @brief (one liner)
  *
  * (documentation goes here)
  */
void FileTransToolsFrame::On_GenFlash_Clicked(wxCommandEvent& event)
{
    bool retVal = m_HexFileTrans.GenFlashFile();
    if(retVal == false)
    {
        wxMessageDialog * dial = new wxMessageDialog(NULL, m_HexFileTrans.GetLES(), _T("Error"), wxOK | wxICON_ERROR);
        dial->ShowModal();
    }
    else
    {
        m_FlashText->SetValue(m_HexFileTrans.GetFlashPath());
    }
}

/** @brief (one liner)
  *
  * (documentation goes here)
  */
void FileTransToolsFrame::On_PrjType_Changed(wxCommandEvent& event)
{
    m_HexFileTrans.SetTypeName(m_PrjTypeCB->GetString(m_PrjTypeCB->GetSelection()).ToStdWstring());
    m_HexFileTrans.SetTypeIdx(HexType(m_PrjTypeCB->GetSelection()));
}

/** @brief (one liner)
  *
  * (documentation goes here)
  */
void FileTransToolsFrame::On_FlashBtn_Clicked(wxCommandEvent& event)
{
      wxFileDialog * openFileDialog = new wxFileDialog(this, _("Open Flash file"), "", "",
                       "Hex files (*.hex)|*.hex", wxFD_OPEN|wxFD_FILE_MUST_EXIST);

  if (openFileDialog->ShowModal() == wxID_OK){
      wxString fileName = openFileDialog->GetPath();
      m_HexFileTrans.SetFlashPath(fileName.ToStdWstring());
      m_FlashText->SetValue(wxString(m_HexFileTrans.GetFlashPath()));
  }
}

/** @brief (one liner)
  *
  * (documentation goes here)
  */
void FileTransToolsFrame::On_BootBtn_Clicked(wxCommandEvent& event)
{
      wxFileDialog * openFileDialog = new wxFileDialog(this, _("Open Boot file"), "", "",
                       "Hex files (*.hex)|*.hex", wxFD_OPEN|wxFD_FILE_MUST_EXIST);

  if (openFileDialog->ShowModal() == wxID_OK){
      wxString fileName = openFileDialog->GetPath();
      m_HexFileTrans.SetBLPath(fileName.ToStdWstring());
      m_BootText->SetValue(wxString(m_HexFileTrans.GetBLPath()));
  }
}

/** @brief (one liner)
  *
  * (documentation goes here)
  */
void FileTransToolsFrame::On_CalBtn_Clicked(wxCommandEvent& event)
{
          wxFileDialog * openFileDialog = new wxFileDialog(this, _("Open Cal file"), "", "",
                       "Cal files (*.hex,*.xlsx,*.xls)|*.hex;*.xlsx;*.xls", wxFD_OPEN|wxFD_FILE_MUST_EXIST);

  if (openFileDialog->ShowModal() == wxID_OK){
      wxString fileName = openFileDialog->GetPath();
      if((fileName.EndsWith(".xlsx")) || (fileName.EndsWith(".xls")))
      {
          m_HexFileTrans.SetCalPath_xlsx(fileName.ToStdWstring());
          m_HexFileTrans.SetCalPath(L"");
      }
      else if(fileName.EndsWith(".hex"))
      {
           m_HexFileTrans.SetCalPath(fileName.ToStdWstring());
           m_HexFileTrans.SetCalPath_xlsx(L"");
      }
      else
      {

      }

      m_CalText->SetValue(fileName);
  }
}


